<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>时间管理大师</title>
    <!-- <iframe height="300px" width="100%" frameborder=0;  style="border: none; padding: 0;margin: 0;" src="http://hackcode.ishoulu.com/linux/"></iframe> -->
<body>
    <div class="full">
        <div class="clock" id="clock">
            <div class="flip down">
                <div class="digital front number0"></div>
                <div class="digital back number1"></div>
            </div>
            <div class="flip down">
                <div class="digital front number0"></div>
                <div class="digital back number1"></div>
            </div>
            <em>:</em>
            <div class="flip down">
                <div class="digital front number0"></div>
                <div class="digital back number1"></div>
            </div>
            <div class="flip down">
                <div class="digital front number0"></div>
                <div class="digital back number1"></div>
            </div>
            <em>:</em>
            <div class="flip down">
                <div class="digital front number0"></div>
                <div class="digital back number1"></div>
            </div>
            <div class="flip down">
                <div class="digital front number0"></div>
                <div class="digital back number1"></div>
            </div>
        </div>
    </div>
</body>
<script>
var flip = document.getElementById('flip')
var backNode = document.querySelector('.back')
var frontNode = document.querySelector('.front')
var btn1 = document.getElementById('btn1')
var btn2 = document.getElementById('btn2')

var count = 0

var isFlipping = false

function flipDown() {

    if (isFlipping) {
        return false
    }

    frontNode.setAttribute('class', 'digital front number' + count)

    var nextCount = count >= 9 ? 0 : (count + 1)

    backNode.setAttribute('class', 'digital back number' + nextCount)

    flip.setAttribute('class', 'flip down go')

    isFlipping = true

    setTimeout(function() {

        flip.setAttribute('class', 'flip down')

        isFlipping = false

        frontNode.setAttribute('class', 'digital front number' + nextCount)

        count = nextCount
    }, 1000)
}

function flipUp() {
    if (isFlipping) {
        return false
    }
    frontNode.setAttribute('class', 'digital front number' + count)
    var nextCount = count <= 0 ? 9 : (count - 1)
    backNode.setAttribute('class', 'digital back number' + nextCount)
    flip.setAttribute('class', 'flip up go')
    isFlipping = true
    setTimeout(function() {
        flip.setAttribute('class', 'flip up')
        isFlipping = false
        frontNode.setAttribute('class', 'digital front number' + nextCount)
        count = nextCount
    }, 1000)
}

function Flipper(config) {

    this.config = {

        node: null,

        frontText: 'number0',

        backText: 'number1',

        duration: 600
    }

    this.nodeClass = {
        flip: 'flip',
        front: 'digital front',
        back: 'digital back'
    }

    Object.assign(this.config, config)

    this.frontNode = this.config.node.querySelector('.front')
    this.backNode = this.config.node.querySelector('.back')

    this.isFlipping = false

    this._init()
}
Flipper.prototype = {
    constructor: Flipper,

    _init: function() {

        this._setFront(this.config.frontText)
        this._setBack(this.config.backText)
    },

    _setFront: function(className) {
        this.frontNode.setAttribute('class', this.nodeClass.front + ' ' + className)
    },

    _setBack: function(className) {
        this.backNode.setAttribute('class', this.nodeClass.back + ' ' + className)
    },
    _flip: function(type, front, back) {

        if (this.isFlipping) {
            return false
        }

        this.isFlipping = true

        this._setFront(front)

        this._setBack(back)

        let flipClass = this.nodeClass.flip;
        if (type === 'down') {
            flipClass += ' down'
        } else {
            flipClass += ' up'
        }

        this.config.node.setAttribute('class', flipClass + ' go')

        setTimeout(() => {

            this.config.node.setAttribute('class', flipClass)

            this.isFlipping = false

            this._setFront(back)
        }, this.config.duration)
    },
    flipDown: function(front, back) {
        this._flip('down', front, back)
    },
    flipUp: function(front, back) {
        this._flip('up', front, back)
    }
}

let clock = document.getElementById('clock')
let flips = clock.querySelectorAll('.flip')
let now = new Date()
let nowTimeStr = formatDate(now, 'hhiiss')
let nextTimeStr = formatDate(new Date(now.getTime() + 1000), 'hhiiss')
let flipObjs = []
for (let i = 0; i < flips.length; i++) {
    flipObjs.push(new Flipper({
        node: flips[i],
        frontText: 'number' + nowTimeStr[i],
        backText: 'number' + nextTimeStr[i]
    }))
}

setInterval(function() {
    let now = new Date()
    let nowTimeStr = formatDate(new Date(now.getTime() - 1000), 'hhiiss')
    let nextTimeStr = formatDate(now, 'hhiiss')
    for (let i = 0; i < flipObjs.length; i++) {
        if (nowTimeStr[i] === nextTimeStr[i]) {
            continue
        }
        flipObjs[i].flipDown('number' + nowTimeStr[i], 'number' + nextTimeStr[i])
    }
}, 1000)

function formatDate(date, dateFormat) {
    if (/(y+)/.test(dateFormat)) {
        dateFormat = dateFormat.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'm+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'i+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(dateFormat)) {
            let str = o[k] + '';
            dateFormat = dateFormat.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return dateFormat;
};
function padLeftZero(str) {
    return ('00' + str).substr(str.length);
}

</script>
<style>
*{
    padding: 0;
    margin: 0;
}
.full{
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: black;
}

.flip {
    display: inline-block;
    position: relative;
    width: 60px;
    height: 100px;
    line-height: 100px;
    border: solid 1px #000;
    border-radius: 10px;
    background: rgb(0, 0, 0);
    font-size: 66px;
    color: #BABABA;
    box-shadow: 0 0 6px rgba(36, 36, 36, 0.5);
    text-align: center;
    font-family: "Helvetica Neue"
}

.flip .digital:before,
.flip .digital:after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    background: #343434;
    overflow: hidden;
    box-sizing: border-box;
}

.flip .digital:before {
    top: 0;
    bottom: 50%;
    border-radius: 10px 10px 0 0;
    border-bottom: solid 1px #666;
}

.flip .digital:after {
    top: 50%;
    bottom: 0;
    border-radius: 0 0 10px 10px;
    line-height: 0;
}

.flip.down .front:before {
    z-index: 3;
}

.flip.down .back:after {
    z-index: 2;
    transform-origin: 50% 0%;
    transform: perspective(160px) rotateX(180deg);
}

.flip.down .front:after,
.flip.down .back:before {
    z-index: 1;
}

.flip.down.go .front:before {
    transform-origin: 50% 100%;
    animation: frontFlipDown 0.6s ease-in-out both;
    box-shadow: 0 -2px 6px rgba(255, 255, 255, 0.3);
    backface-visibility: hidden;
}

.flip.down.go .back:after {
    animation: backFlipDown 0.6s ease-in-out both;
}

.flip.up .front:after {
    z-index: 3;
}

.flip.up .back:before {
    z-index: 2;
    transform-origin: 50% 100%;
    transform: perspective(160px) rotateX(-180deg);
}

.flip.up .front:before,
.flip.up .back:after {
    z-index: 1;
}

.flip.up.go .front:after {
    transform-origin: 50% 0;
    animation: frontFlipUp 0.6s ease-in-out both;
    box-shadow: 0 2px 6px rgba(255, 255, 255, 0.3);
    backface-visibility: hidden;
}

.flip.up.go .back:before {
    animation: backFlipUp 0.6s ease-in-out both;
}

@keyframes frontFlipDown {
    0% {
        transform: perspective(160px) rotateX(0deg);
    }

    100% {
        transform: perspective(160px) rotateX(-180deg);
    }
}

@keyframes backFlipDown {
    0% {
        transform: perspective(160px) rotateX(180deg);
    }

    100% {
        transform: perspective(160px) rotateX(0deg);
    }
}


@keyframes frontFlipUp {
    0% {
        transform: perspective(160px) rotateX(0deg);
    }

    100% {
        transform: perspective(160px) rotateX(180deg);
    }
}

@keyframes backFlipUp {
    0% {
        transform: perspective(160px) rotateX(-180deg);
    }

    100% {
        transform: perspective(160px) rotateX(0deg);
    }
}

.flip .number0:before,
.flip .number0:after {
    content: "0";
}

.flip .number1:before,
.flip .number1:after {
    content: "1";
}

.flip .number2:before,
.flip .number2:after {
    content: "2";
}

.flip .number3:before,
.flip .number3:after {
    content: "3";
}

.flip .number4:before,
.flip .number4:after {
    content: "4";
}

.flip .number5:before,
.flip .number5:after {
    content: "5";
}

.flip .number6:before,
.flip .number6:after {
    content: "6";
}

.flip .number7:before,
.flip .number7:after {
    content: "7";
}

.flip .number8:before,
.flip .number8:after {
    content: "8";
}

.flip .number9:before,
.flip .number9:after {
    content: "9";
}

.clock {
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -5em;
    margin-left: -14em;
    transform:scale(3);
}

.clock em {
    display: inline-block;
    line-height: 102px;
    font-size: 66px; 
    color: #BABABA;
    font-style: normal;
    vertical-align: top;
}
</style>

</html>